package com.yunshipei.ui.fragment;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.yunshipei.ui.activity.AlarmSetActivity;
import com.yunshipei.enterplorer.R;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.ui.view.wheelview.OnWheelScrollListener;
import com.yunshipei.ui.view.wheelview.WheelView;
import com.yunshipei.ui.view.wheelview.adapter.NumericWheelAdapter;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * A simple {@link Fragment} subclass.
 */
public class TimeFragment extends BaseFragment {
    private LinearLayout content = null;
    private View view;
    private WheelView year;
    private WheelView month;
    private WheelView day;
    private AlarmSetActivity alarmSetActivity = null;
    private NotifyInfo notifiInfo = null;
    private DecimalFormat df = new DecimalFormat("00");
    NumericWheelAdapter dayNumericWheelAdapter;

    public TimeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        alarmSetActivity = (AlarmSetActivity) activity;
        notifiInfo = alarmSetActivity.getNotifiInfo();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View Contentview = inflater.inflate(R.layout.fragment_time, container, false);
        content = (LinearLayout) Contentview.findViewById(R.id.content);
        initView();
        return Contentview;
    }

    public void initView() {
        content.addView(getDataPick());
    }

    private View getDataPick() {
        Calendar calendar = Calendar.getInstance();
        if (notifiInfo != null) {
            Date date = notifiInfo.getSpeechRecognition().getDateTime().getInfactDate();
            calendar.setTime(date);
            Log.e("时间格式", calendar.get(Calendar.YEAR) + "   " + (calendar.get(Calendar.MONTH) + 1) + "    " + calendar.get(Calendar.DATE));
        }
        int norYear = Calendar.getInstance().get(Calendar.YEAR) + 1;
        //获取时间滚轮
        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.wheel_time_picker_set, null);
        year = (WheelView) view.findViewById(R.id.wheel_year);
        Log.e("norYear", "" + (norYear));
        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(getActivity(), 2000, norYear);
        numericWheelAdapter1.setLabel("年");
        year.setViewAdapter(numericWheelAdapter1);
        year.setCyclic(true);
        year.addScrollingListener(scrollListener);

        month = (WheelView) view.findViewById(R.id.wheel_mooth);
        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(getActivity(), 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCyclic(true);
        month.addScrollingListener(scrollListener);
        day = (WheelView) view.findViewById(R.id.wheel_day);
        initDay(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
        day.setCyclic(true);
        day.addScrollingListener(scrollListener);

        year.setVisibleItems(7);
        month.setVisibleItems(7);
        day.setVisibleItems(7);
        Log.e("时间 年", calendar.get(Calendar.YEAR) + " " + (calendar.get(Calendar.MONTH) + 1) + " " + calendar.get(Calendar.DATE));
        year.setCurrentItem(calendar.get(Calendar.YEAR) - 2000);
        month.setCurrentItem(calendar.getTime().getMonth());
        day.setCurrentItem(calendar.get(Calendar.DATE) -1);
        return view;
    }

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            if (wheel == month) {
                initDay(year.getCurrentItem() + 2000, month.getCurrentItem() + 1);
                day.setCurrentItem(0);
            }
            handlerSendMessage();
            Log.e("结果时间", "年:" + (year.getCurrentItem() + 2000) + "   月:" + month.getCurrentItem() + 1 + " 日" + (day.getCurrentItem() + 1));
        }
    };

    /**
     */
    private void initDay(int arg1, int arg2) {
        dayNumericWheelAdapter = new NumericWheelAdapter(getActivity(), 1, getDay(arg1, arg2), "%02d");
        dayNumericWheelAdapter.setLabel("日");
        day.setViewAdapter(dayNumericWheelAdapter);
    }

    /**
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    @Override
    void handlerSendMessage() {
        Message message = new Message();
        message.what = AlarmSetActivity.DATETIME;
        Bundle bundle = new Bundle();
        bundle.putString("year", String.valueOf(year.getCurrentItem() + 2000));
        bundle.putString("month", df.format(month.getCurrentItem() + 1));
        bundle.putString("day", df.format(day.getCurrentItem() + 1));
        message.setData(bundle);
        handler.sendMessage(message);
    }

}